﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Util.Helpers;

namespace Edu.Web.Controllers
{
    public class CourceScoreController : BaseController
    {
        private readonly ICourceExamService _courceExamService;
        private readonly ITimeTableService _timeTableService;
        private readonly IClazzService _clazzService;

        public CourceScoreController(ICourceExamService courceExamService, ITimeTableService timeTableService, IClazzService clazzService)
        {
            _courceExamService = courceExamService;
            _timeTableService = timeTableService;
            _clazzService = clazzService;
        }

        // GET: CourceScore
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {

            return View();
        }

        public ActionResult List4Stu()
        {
            return View();
        }

        public ActionResult List4Teacher()
        {
            return View();
        }

        /// <summary>
        /// 获取学生成绩
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetScoreList4Stu()
        {
            var scoreList = _courceExamService.GetCourceScoreByStuId(CurrentUser.UserId);
            return Json(scoreList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取学生成绩
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetScoreList4Teacher()
        {
            var scoreList = _courceExamService.GetCourceScoreByCreateUserId(CurrentUser.UserId);
            return Json(scoreList, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetCourceList()
        {
            return Json(_timeTableService.GetCourceProjectListByTeacherId(CurrentUser.UserId).Select(x => new { CourceId = x.CourceId, CourceName = x.CourceName }).Distinct(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetClazzList(long courceId)
        {
            return Json(_timeTableService.GetClazzListByCourceId(courceId, CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetStuList(long clazzId)
        {
            return Json(_clazzService.LoadClazzStuByClazzId(clazzId), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult SaveExam()
        {
            try
            {
                Dictionary<string, object> ce = GetRequestMap();
                CourceExamEntity courceExamVO = RequestMapHelper.GetMapModel<CourceExamEntity>(ce);
                List<CourceExamScoreEntity> courceExamScoreList = RequestMapHelper.GetMapModels<CourceExamScoreEntity>(ce["CourceExamScoreList"]);
                foreach (var courceExamScore in courceExamScoreList)
                {
                    if (courceExamScore.Score <= 0)
                    {
                        throw new Exception("请输入输入分数！");
                    }
                }
                courceExamVO.CreateTime = DateTime.Now;
                courceExamVO.CreateUserId = CurrentUser.UserId;
                _courceExamService.SaveExam(courceExamVO, courceExamScoreList);

                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }
    }
}